package com.zsd.android.library.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;

import java.util.ArrayList;

public class PermissionHelper {

    public static final int PERMISSION_WRITER = 0x0001;
    public static final int PERMISSION_READ = 0x0002;
    public static final int PERMISSION_READ_CONTACTS = 0x0003;
    public static final int PERMISSION_CAMERA = 0x0004;
    public static final int PERMISSION_LOCATION = 0x0005;
    public static final int PERMISSION_SMS = 0x0006;
    public static final int PERMISSION_RECORDER = 0x0007;
    public static final int PERMISSION_VOICE = 0x0008;
    private static volatile PermissionHelper mHelper;
    private Context mContext;

    private PermissionHelper(Context mContext) {
        this.mContext = mContext.getApplicationContext();
    }

    public static PermissionHelper getInstance(Context mContext) {
        PermissionHelper permissionHelper = mHelper;
        if (permissionHelper == null) {
            synchronized (PermissionHelper.class) {
            if (permissionHelper == null) {
                    permissionHelper = new PermissionHelper(mContext);
                    mHelper = permissionHelper;
                }
            }
        }
        return permissionHelper;
    }

    public boolean isPermission(Object o, String[] permissions, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissionList = needPermission(permissions);
            int size = permissionList.size();
            if (size > 0) {
                if (o instanceof Activity) {
                    ActivityCompat.requestPermissions((Activity) o, permissionList.toArray(new String[size]), requestCode);
                } else if (o instanceof Fragment) {
                    ((Fragment)o).requestPermissions(permissionList.toArray(new String[size]), requestCode);
                }
                return false;
            }
        }
        return true;
    }

    private ArrayList<String> needPermission(String ... permissions) {
        ArrayList<String> permissionList = new ArrayList<>();
        for (String permission : permissions) {
            int checkSelfPermission = ContextCompat.checkSelfPermission(mContext, permission);
            if (PackageManager.PERMISSION_GRANTED != checkSelfPermission) {
                permissionList.add(permission);
            }
        }
        return permissionList;
    }

    public void showMissingPermissionDialog(final Context mContext, String msg) {
        AlertDialog dialog = new AlertDialog.Builder(mContext)
                .setTitle("提示")
                .setMessage(msg)
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                startAppSettings(mContext);
                            }
                        })

                .setCancelable(false)
                .create();
        dialog.show();
    }

    private void startAppSettings(Context mContext) {
        Intent intent = new Intent(
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + mContext.getPackageName()));
        mContext.startActivity(intent);
    }
}
